package com.mayikt.dao;

import com.mayikt.entity.MayiktUserEntity;
import com.mayikt.utils.MayiktJdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MayikUsertDao {
    public MayiktUserEntity login(String userName,String userPwd){
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = MayiktJdbcUtils.getConnection();
            String sql = "String sql = \"SELECT * FROM mayikt_users WHERE userName = ? AND userPwd = ?\";";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,userName);
            pstmt.setString(2,userPwd);
            rs = pstmt.executeQuery();
            if(!rs.next()){
                return null;
            }
            Integer id = rs.getInt(1);
            String dbuserName = rs.getString(2);
            String dbuserPwd = rs.getString(3);
            MayiktUserEntity mayiktUserEntity = new MayiktUserEntity(id , dbuserName,dbuserPwd);
            return mayiktUserEntity;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            MayiktJdbcUtils.closeConnection(rs,pstmt,conn);
        }
    }
    public int register(String userName,String userPwd){
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = MayiktJdbcUtils.getConnection();
            MayiktJdbcUtils.beginTransaction(conn);
            String insertSql = "INSERT INTO `flight`.`mayikt_users` (`id`, `userName`, `userPwd`)" +
                    " VALUES (null, ?, ?);";
            pstmt = conn.prepareStatement(insertSql);
            pstmt.setString(1,userName);
            pstmt.setString(2,userPwd);
            int result = pstmt.executeUpdate();
            MayiktJdbcUtils.commitTransaction(conn);
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
            MayiktJdbcUtils.rollBackTransaction(conn);
            return 0;
        }finally {
            MayiktJdbcUtils.closeConnection(pstmt,conn);
        }

    }
    public MayiktUserEntity findByUserName(String userName) {
        ResultSet resultSet = null;
        PreparedStatement preparedStatement = null;
        Connection connection = null;
        try {
            connection = MayiktJdbcUtils.getConnection();
            String loginSql = "select  * from  mayikt_users where userName=?";
            preparedStatement = connection.prepareStatement(loginSql);
            preparedStatement.setString(1, userName);
            resultSet = preparedStatement.executeQuery();
            if (!resultSet.next()) {  // 查询不到用户数据
                return null;
            }
            // 将db中数据 返回给客户端 查询到数据
            Integer id = resultSet.getInt(1);
            String dbUserName = resultSet.getString(2);
            String dbUserPwd = resultSet.getString(3);
            MayiktUserEntity mayiktUserEntity = new MayiktUserEntity(id, dbUserName, dbUserPwd);
            return mayiktUserEntity;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            MayiktJdbcUtils.closeConnection(resultSet, preparedStatement, connection);
        }
    }
}
